<HTML>
<BODY BGCOLOR="white">
<PRE>
<FONT color="green">001</FONT>    package org.hackystat.systemstatus.pingmail;<a name="line.1"></a>
<FONT color="green">002</FONT>    <a name="line.2"></a>
<FONT color="green">003</FONT>    import java.util.Date;<a name="line.3"></a>
<FONT color="green">004</FONT>    import java.util.Properties;<a name="line.4"></a>
<FONT color="green">005</FONT>    <a name="line.5"></a>
<FONT color="green">006</FONT>    import javax.mail.Message;<a name="line.6"></a>
<FONT color="green">007</FONT>    import javax.mail.Session;<a name="line.7"></a>
<FONT color="green">008</FONT>    import javax.mail.Transport;<a name="line.8"></a>
<FONT color="green">009</FONT>    import javax.mail.internet.InternetAddress;<a name="line.9"></a>
<FONT color="green">010</FONT>    import javax.mail.internet.MimeMessage;<a name="line.10"></a>
<FONT color="green">011</FONT>    <a name="line.11"></a>
<FONT color="green">012</FONT>    import org.hackystat.sensorbase.client.SensorBaseClient;<a name="line.12"></a>
<FONT color="green">013</FONT>    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;<a name="line.13"></a>
<FONT color="green">014</FONT>    import org.hackystat.utilities.stacktrace.StackTrace;<a name="line.14"></a>
<FONT color="green">015</FONT>    <a name="line.15"></a>
<FONT color="green">016</FONT>    /**<a name="line.16"></a>
<FONT color="green">017</FONT>     * Demonstration class to show a simple form of service monitoring. <a name="line.17"></a>
<FONT color="green">018</FONT>     * Provides a main class that is invoked with a mail server, a SensorBase URL, and an email<a name="line.18"></a>
<FONT color="green">019</FONT>     * address. The hackystat server is pinged and an email is generated indicating whether the ping was<a name="line.19"></a>
<FONT color="green">020</FONT>     * successful. If so, the server is up and accepting requests.  <a name="line.20"></a>
<FONT color="green">021</FONT>     * <a name="line.21"></a>
<FONT color="green">022</FONT>     * @author Philip Johnson<a name="line.22"></a>
<FONT color="green">023</FONT>     */<a name="line.23"></a>
<FONT color="green">024</FONT>    public class PingMail {<a name="line.24"></a>
<FONT color="green">025</FONT>    <a name="line.25"></a>
<FONT color="green">026</FONT>      /**<a name="line.26"></a>
<FONT color="green">027</FONT>       * When invoked, this main class pings the passed server and sends an email indicating whether the<a name="line.27"></a>
<FONT color="green">028</FONT>       * ping was successful or not to the passed email address via the passed mail server.<a name="line.28"></a>
<FONT color="green">029</FONT>       * <a name="line.29"></a>
<FONT color="green">030</FONT>       * @param args Takes three string arguments, a hackystat server, a mail server, and an email.<a name="line.30"></a>
<FONT color="green">031</FONT>       */<a name="line.31"></a>
<FONT color="green">032</FONT>      public static void main(String[] args) {<a name="line.32"></a>
<FONT color="green">033</FONT>        <a name="line.33"></a>
<FONT color="green">034</FONT>        // Make sure we have correct number of args. <a name="line.34"></a>
<FONT color="green">035</FONT>        if (!(args.length == 3)) {<a name="line.35"></a>
<FONT color="green">036</FONT>          System.out.println("java -jar pingmail.jar &lt;sensorbase&gt; &lt;mail server&gt; &lt;email&gt;");<a name="line.36"></a>
<FONT color="green">037</FONT>          return;<a name="line.37"></a>
<FONT color="green">038</FONT>        }<a name="line.38"></a>
<FONT color="green">039</FONT>        // Assign the args to variables for readability. <a name="line.39"></a>
<FONT color="green">040</FONT>        String sensorbase = args[0];<a name="line.40"></a>
<FONT color="green">041</FONT>        String mailServer = args[1];<a name="line.41"></a>
<FONT color="green">042</FONT>        String email = args[2];<a name="line.42"></a>
<FONT color="green">043</FONT>        // Echo what we're going to do. <a name="line.43"></a>
<FONT color="green">044</FONT>        System.out.println("Contacting sensorbase server at: " + sensorbase);<a name="line.44"></a>
<FONT color="green">045</FONT>        System.out.println("Emailing results to " + email);<a name="line.45"></a>
<FONT color="green">046</FONT>        System.out.println("Using mail server " + mailServer);<a name="line.46"></a>
<FONT color="green">047</FONT>        // For demo purposes, use the pre-defined test user and password. <a name="line.47"></a>
<FONT color="green">048</FONT>        String user = "TestUser@hackystat.org";<a name="line.48"></a>
<FONT color="green">049</FONT>        String password = "TestUser@hackystat.org";<a name="line.49"></a>
<FONT color="green">050</FONT>        <a name="line.50"></a>
<FONT color="green">051</FONT>        // Start doing some checks. <a name="line.51"></a>
<FONT color="green">052</FONT>        boolean isHost = SensorBaseClient.isHost(sensorbase);<a name="line.52"></a>
<FONT color="green">053</FONT>        boolean isRegistered = SensorBaseClient.isRegistered(sensorbase, user, password);<a name="line.53"></a>
<FONT color="green">054</FONT>        SensorBaseClient client = new SensorBaseClient(sensorbase, user, password);<a name="line.54"></a>
<FONT color="green">055</FONT>        SensorDataIndex index;<a name="line.55"></a>
<FONT color="green">056</FONT>        boolean hasData;<a name="line.56"></a>
<FONT color="green">057</FONT>        try {<a name="line.57"></a>
<FONT color="green">058</FONT>          index = client.getSensorDataIndex(user);<a name="line.58"></a>
<FONT color="green">059</FONT>          hasData = (index.getSensorDataRef().size() &gt; 0);<a name="line.59"></a>
<FONT color="green">060</FONT>        }<a name="line.60"></a>
<FONT color="green">061</FONT>        catch (Exception e) {<a name="line.61"></a>
<FONT color="green">062</FONT>          hasData = false;<a name="line.62"></a>
<FONT color="green">063</FONT>        }<a name="line.63"></a>
<FONT color="green">064</FONT>        // Create report on results. <a name="line.64"></a>
<FONT color="green">065</FONT>        String hostReport = "Attempt to contact " + sensorbase + (isHost ? " succeeds" : " fails");<a name="line.65"></a>
<FONT color="green">066</FONT>        String userReport = "Attempt to login " + user + (isRegistered ? " succeeds" : " fails");<a name="line.66"></a>
<FONT color="green">067</FONT>        String dataReport = "Attempt to get data " + (hasData ? "succeeds" : "fails");<a name="line.67"></a>
<FONT color="green">068</FONT>        String report = hostReport + "\n" + userReport + "\n" + dataReport;<a name="line.68"></a>
<FONT color="green">069</FONT>        System.out.println(report);<a name="line.69"></a>
<FONT color="green">070</FONT>        sendMail(mailServer, email, "Hackystat PingMail (" + (hasData ? "OK" : "FAIL") + ")" , report);<a name="line.70"></a>
<FONT color="green">071</FONT>      }<a name="line.71"></a>
<FONT color="green">072</FONT>    <a name="line.72"></a>
<FONT color="green">073</FONT>      /**<a name="line.73"></a>
<FONT color="green">074</FONT>       * Sends email to the userEmail.<a name="line.74"></a>
<FONT color="green">075</FONT>       * @param mailServer The mail server. <a name="line.75"></a>
<FONT color="green">076</FONT>       * @param userEmail The userEmail.<a name="line.76"></a>
<FONT color="green">077</FONT>       * @param subject The subject of the email.<a name="line.77"></a>
<FONT color="green">078</FONT>       * @param body The body of the email.<a name="line.78"></a>
<FONT color="green">079</FONT>       */<a name="line.79"></a>
<FONT color="green">080</FONT>      private static void sendMail(String mailServer, String userEmail, String subject, String body) {<a name="line.80"></a>
<FONT color="green">081</FONT>        Properties props = new Properties();<a name="line.81"></a>
<FONT color="green">082</FONT>        props.put("mail.smtp.host", mailServer);<a name="line.82"></a>
<FONT color="green">083</FONT>        Session session = Session.getInstance(props);<a name="line.83"></a>
<FONT color="green">084</FONT>        try {<a name="line.84"></a>
<FONT color="green">085</FONT>          Message msg = new MimeMessage(session);<a name="line.85"></a>
<FONT color="green">086</FONT>          InternetAddress userAddress = new InternetAddress(userEmail);<a name="line.86"></a>
<FONT color="green">087</FONT>          InternetAddress[] adminAddressArray = { userAddress };<a name="line.87"></a>
<FONT color="green">088</FONT>          msg.setFrom(userAddress);<a name="line.88"></a>
<FONT color="green">089</FONT>          msg.setReplyTo(adminAddressArray);<a name="line.89"></a>
<FONT color="green">090</FONT>          msg.setRecipient(Message.RecipientType.TO, userAddress);<a name="line.90"></a>
<FONT color="green">091</FONT>          msg.setSubject(subject);<a name="line.91"></a>
<FONT color="green">092</FONT>          msg.setSentDate(new Date());<a name="line.92"></a>
<FONT color="green">093</FONT>          msg.setText(body);<a name="line.93"></a>
<FONT color="green">094</FONT>          Transport.send(msg);<a name="line.94"></a>
<FONT color="green">095</FONT>          System.out.println("Email regarding system status sent to " + userEmail);<a name="line.95"></a>
<FONT color="green">096</FONT>        }<a name="line.96"></a>
<FONT color="green">097</FONT>        catch (Exception e) {<a name="line.97"></a>
<FONT color="green">098</FONT>          String msg = "Error sending mail. Perhaps JavaMail is not installed correctly? " +<a name="line.98"></a>
<FONT color="green">099</FONT>          "See http://code.google.com/p/hackystat/wiki/InstallingJavaMail for instructions." +<a name="line.99"></a>
<FONT color="green">100</FONT>          StackTrace.toString(e);<a name="line.100"></a>
<FONT color="green">101</FONT>          System.err.println(msg);<a name="line.101"></a>
<FONT color="green">102</FONT>        }<a name="line.102"></a>
<FONT color="green">103</FONT>      }<a name="line.103"></a>
<FONT color="green">104</FONT>    }<a name="line.104"></a>




























































</PRE>
</BODY>
</HTML>
